//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.goodscoupon;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.commonunits.alarm.alarmclock.AlarmClock;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.customer.support.CustomerData;
import com.restaurant.application.genshanguser.GenShangUser;
import com.restaurant.application.genshanguser.support.GenShangUserData;
import com.restaurant.application.goodscoupon.support.AbstractGoodsCoupon;
import com.restaurant.application.goodscoupon.support.GoodsCouponDetail;
import com.restaurant.application.indexes.goodscouponlist.mysql.GoodsCouponListBean;
import com.restaurant.application.indexes.goodscouponlist.mysql.GoodsCouponListImpl;
import com.restaurant.application.qrcode.QrCode;
import com.restaurant.application.util.CommonUtils;
import java.util.Objects;

public class GoodsCouponImpl extends AbstractGoodsCoupon {
    public GoodsCouponImpl() {
    }

    public View getFullView() {
        return null;
    }

    public View getViewLead(ViewSpec spec) {
        return null;
    }

    public void init(GoodsCouponDetail data) {
        QrCode qrUid = (QrCode)this.createNode(QrCode.class.getName());
        String qrUrl = CommonUtils.erCodeGenerate(data.getNumber());
        qrUid.createQr(qrUrl, this.getId(), data.getNumber(), 0);
        data.withCreateTime(DateTimeUtils.getCurrentMillisecond()).withQrCodeUid(qrUid.getId()).withVersion(0).withQrCodeUrl(qrUrl).withState("未使用").withUseful("有效");
        this.setData(data);
        this.addCustomer();
        this.insert();
        if (Objects.nonNull(this.getData().getEndTime())) {
            ((AlarmClock)this.getSingleton(AlarmClock.class)).setDelayAlarmClock("goodsCoupon" + this.getId(), this.getData().getEndTime() - DateTimeUtils.getCurrentMillisecond(), this.getId());
        }

    }

    private void insert() {
        GoodsCouponListBean bean = this.getGoodsCouponDataForGoodsCouponList(new GoodsCouponListBean());
        ((GoodsCouponListImpl)this.getIndex(GoodsCouponListImpl.class)).insert(bean);
    }

    public String confirmReceive(int version) {
        Boolean aBoolean = this.checkState();
        if (aBoolean) {
            return "该券已使用或无效";
        } else if (version != this.getData().getVersion()) {
            return "该券已领取！";
        } else {
            this.addCustomer();
            return "success";
        }
    }

    public String confirm() {
        if (!Objects.equals(this.getData().getState(), "已使用") && !Objects.equals(this.getData().getUseful(), "无效")) {
            this.getData().setState("已使用");
            this.getData().setUseful("无效");
            UID gate = this.getGate();
            GenShangUserData data = ((GenShangUser)this.getUnit(GenShangUser.class, gate)).findData();
            this.getData().withCashierId(data.getNumber()).withCashierName(data.getNickName()).withCashierUid(gate);
            this.confirmUsed();
            return "success";
        } else {
            return "已使用或无效";
        }
    }

    public void upStateUseful(String useful) {
        if (!Objects.equals("无效", this.getData().getUseful())) {
            this.getData().setUseful("无效");
        }

    }

    public String perfectData(GoodsCouponDetail detail) {
        if (!Strings.isNullOrEmpty(detail.getAddress())) {
            this.getData().setAddress(detail.getAddress());
        }

        if (!Strings.isNullOrEmpty(detail.getType())) {
            this.getData().setType(detail.getType());
            if (Objects.equals(detail.getType(), "物流配送")) {
                this.getData().setDeliveryState("待配送");
                this.getData().setOrderNumber(CommonUtils.getOderNumber(this));
            }
        }

        return "success";
    }

    public GoodsCouponDetail getGoodsDetail() {
        GoodsCouponDetail duplicate = this.getData().duplicate();
        if (!Strings.isNullOrEmpty(duplicate.getType())) {
            duplicate.withType(duplicate.getType().substring(2));
        }

        return duplicate;
    }

    public void updateLogistic(String data) {
        this.getData().setDeliveryState("已发货");
        this.getData().setLogistical(data);
        this.quickIndexUpdate();
    }

    private void confirmUsed() {
        this.getData().setUsedCustomerId(this.getData().getCurrentCustomerId());
        this.getData().setUsedCustomerName(this.getData().getCurrentCustomerName());
        this.getData().setUsedPhone(this.getData().getCurrentPhone());
        this.getData().setUsedCustomerUid(this.getData().getCurrentCustomerUid());
        this.getData().setUsedTime(DateTimeUtils.getCurrentMillisecond());
    }

    private void addCustomer() {
        Boolean aBoolean = this.checkState();
        if (!aBoolean) {
            UID gate = this.getGate();
            CustomerData customerData = ((Customer)this.getUnit(Customer.class, gate)).findData();
            if (Objects.equals(this.getData().getVersion(), 0)) {
                this.getData().withFirstCustomerId(customerData.getNumber()).withFirstCustomerName(customerData.getNickName()).withFirstPhone(customerData.getPhone()).withFirstCustomerUid(gate);
            }

            this.getData().withCurrentCustomerId(customerData.getNumber()).withCurrentCustomerName(customerData.getNickName()).withCurrentPhone(customerData.getPhone()).withCurrentCustomerUid(gate);
            int version = this.getData().getVersion() + 1;
            this.getData().withVersion(version);
        }
    }

    Boolean checkState() {
        return Objects.equals(this.getData().getState(), "已使用") || Objects.equals(this.getData().getUseful(), "无效");
    }

    public GoodsCouponListBean getGoodsCouponDataForGoodsCouponList(GoodsCouponListBean data) {
        if (Objects.nonNull(this.getData().getCurrentCustomerUid())) {
            data.withCurrentCustomerUid(this.getData().getCurrentCustomerUid().value());
        }

        return data.withCreateTime(this.getData().getCreateTime()).withGoodsCoupon(this.getId()).withGoodsUID(this.getData().getGoodsUID().value()).withName(this.getData().getName()).withCouponNumber(this.getData().getNumber()).withFirstCustomerId(this.getData().getCurrentCustomerId()).withFirstPhone(this.getData().getFirstPhone()).withUseful(this.getData().getUseful()).withState(this.getData().getState()).withUsedTime(this.getData().getUsedTime()).withUsedCustomerId(this.getData().getUsedCustomerId()).withUsedPhone(this.getData().getUsedPhone()).withCashierName(this.getData().getCashierName()).withType(this.getData().getType()).withAddress(this.getData().getAddress()).withGoodsId(this.getData().getId()).withOrderNumber(this.getData().getOrderNumber()).withCurrentName(this.getData().getCurrentCustomerName()).withCurrentPhone(this.getData().getCurrentPhone()).withLogistical(this.getData().getLogistical()).withDeliveryState(this.getData().getDeliveryState());
    }

    public boolean timerCall(String name) {
        if (Objects.equals(name, "goodsCoupon" + this.getId())) {
            this.getData().setUseful("无效");
            return true;
        } else {
            return false;
        }
    }
}
